<template>
  <div class="flex h100vh login relative">
    <div class="absolute login-main">
      <h1 class="text-center mb-16">Lvlu</h1>
      <n-form
        ref="formRef"
        :model="form"
        :rules="rules"
      >
        <n-form-item path="username">
          <n-input
            v-model:value="form.username"
            type="text"
            placeholder="用户名"
          >
            <template #prefix>
              <n-icon :component="PersonOutline" />
            </template>
          </n-input>
        </n-form-item>
        <n-form-item path="password">
          <n-input
            v-model:value="form.password"
            ref="passwordInput"
            :type="pswType"
            placeholder="密码"
          >
            <template #prefix>
              <n-icon :component="LockClosedOutline" />
            </template>
            <template #suffix>
              <n-icon
                class="cursor-pointer"
                :component="pswIcon"
                @click="togglePsw"
              />
            </template>
          </n-input>
        </n-form-item>
        <n-form-item>
          <n-button
            type="primary"
            class="w100"
            @click="handleLogin"
          >
            登录
          </n-button>
        </n-form-item>
      </n-form>

      <div class="text-center c-666">忘记密码？</div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { loginUser } from '@/api/login'
  import type { FormInst, FormRules, FormItemRule, InputInst } from 'naive-ui'
  import { LockClosedOutline, PersonOutline, EyeOutline, EyeOffOutline } from '@vicons/ionicons5'
  import { tokenUtil } from '@/utils/auth'
  import { encrypt } from '@/utils/secret'
  import { useRouter } from 'vue-router'
  const router = useRouter()

  const form = ref({
    username: import.meta.env.DEV ? 'admin':'',
    password: import.meta.env.DEV ? '123456':'',
  })

  const formRef = ref<FormInst | null>(null)

  const rules: FormRules = {
    username: {
      validator: (rule: FormItemRule, value: string) => {
        if (!value) return new Error('请输入用户名')
        return true
      }
    },
    password: {
      validator: (rule: FormItemRule, value: string) => {
        if (!value) return new Error('请输入密码')
        return true
      }
    }
  }

  const showPsw = ref(false)
  const passwordInput = ref<InputInst | null>(null)

  const togglePsw = () => {
    showPsw.value = !showPsw.value
    passwordInput.value?.blur()
    nextTick(() => {
      passwordInput.value?.focus()
    })
  }

  const pswIcon = computed(() => (showPsw.value ? EyeOutline : EyeOffOutline))

  const pswType = computed(() => (showPsw.value ? 'text' : 'password'))

  // 登录
  const handleLogin = () => {
    formRef.value?.validate(async valid => {
      if (valid) return
      const submitForm = Object.assign({}, form.value)
      submitForm.password = encrypt(submitForm.password)
      const res = await loginUser(submitForm)
      tokenUtil.setToken(res.data.token)
      router.push('/')
    })
  }
</script>

<style scoped lang="scss">
  .login {
    background: url('/src/assets/image/login.webp');
    background-size: 100% 100%;
    &-main {
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 400px;
      height: 400px;
      background-color: #fff;
      border-radius: 8px;
      padding: 24px;
      box-sizing: border-box;
    }
  }
</style>
